package com.lsh.payment.core.util;

import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.UriInfo;
import java.util.List;
import java.util.Map;

public class Criteria {

    private MultivaluedMap<String, String> criteria = new MultivaluedHashMap<>();

    public Criteria() {
    }

    public Criteria(MultivaluedMap<String, String> parameters) {
        for (Map.Entry<String, List<String>> entry : parameters.entrySet()) {
            criteria.put(formatKey(entry.getKey()), entry.getValue());
        }
    }

    public static Criteria of(UriInfo uriInfo) {
        return new Criteria(uriInfo.getQueryParameters());
    }

    public static Criteria empty() {
        return new Criteria();
    }

    public MultivaluedMap<String, String> getCriteria() {
        return criteria;
    }

    public List<String> get(String key) {
        return criteria.get(formatKey(key));
    }

    public String getFirst(String key) {
        return criteria.getFirst(formatKey(key));
    }

    public void add(String key, String value) {
        criteria.add(formatKey(key), value);
    }

    public void replace(String key, String value) {
        criteria.remove(formatKey(key));
        criteria.addFirst(formatKey(key), value);
    }

    private String formatKey(String key) {
        return key.replace("-", "_");
    }
}
